//
//  TMGHelpCenterViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/11.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGHelpCenterViewController.h"
#import "TMGMineTableViewCell.h"
#import "TMGHelpTableViewCell.h"
#import "TMGHelpChildViewController.h"
#define cellImageName @"cellImageName"
#define cellTextString @"cellTextString"
@interface TMGHelpCenterViewController ()<STActionSheetDelegate>
@property(nonatomic, strong) NSArray                     *dataSocue;
@property(nonatomic, strong) NSMutableArray                     *quesArray;
@property(nonatomic, strong) NSIndexPath     *slectedindexPath;
@end

@implementation TMGHelpCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帮助中心";
    [self st_setRightItemWithTitle:@"联系客服" titleColor:[UIColor whiteColor]];
    [self.tableView registerClass:[TMGHelpTableViewCell class] forCellReuseIdentifier:@"section0"];
    [self.tableView registerClass:[TMGMineTableViewCell class] forCellReuseIdentifier:@"section1"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self sendQuestionRequest];
   // [self configDataSouce];
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
    TMGHelpModel * model = [TMGHelpModel new];
//    model.questions = @"黄金口袋安全吗？";
//    model.anwsers = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
//
//    TMGHelpModel * model1 = [TMGHelpModel new];
//    model1.questions = @"存黄金为什么有利息？";
//    model1.anwsers = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
//
//    TMGHelpModel * model2 = [TMGHelpModel new];
//    model2.questions = @"黄金钱包的口袋在哪里？";
//    model2.anwsers = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
//    TMGHelpModel * model3 = [TMGHelpModel new];
//    model3.questions = @"电子合同是神马？";
//    model3.anwsers = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    NSArray * sectionOne = self.quesArray;
   
    NSArray * sectionTwo = @[
                              [[STTableEasyModel alloc]initWithTextString:@"买金和卖金" imageName:@"广州买楼通_底栏_佣金"],
                               [[STTableEasyModel alloc]initWithTextString:@"存金和提金" imageName:@"提金" ],
//                               [[STTableEasyModel alloc]initWithTextString:@"利益和收益" imageName:@"利息-icon" ],
                               [[STTableEasyModel alloc]initWithTextString:@"支付和现金" imageName:@"支付" ],
                               [[STTableEasyModel alloc]initWithTextString:@"红包和优惠券" imageName:@"红包" ]];
    self.dataSocue = @[sectionOne,sectionTwo];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSocue.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSocue[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 44;
    }
    
    TMGHelpModel * model = self.dataSocue[indexPath.section][indexPath.row];
    TMGHelpTableViewCell * cell = (TMGHelpTableViewCell*)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    cell.model = model;
    if (model.isSelected) {
        return model.cellHeight;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    whiteView.backgroundColor = self.tableView.backgroundColor;

    UIView * yeallowView = [[UIView alloc] initWithFrame:CGRectMake(15,0 +10 , 4, 15 )];
    yeallowView.backgroundColor = TM_orangeColor;
    yeallowView.centerY = 22;
    [whiteView addSubview:yeallowView];
    
    NSString * title = (section == 0)?@"常见问题":@"问题分类";
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(yeallowView.right + 15, yeallowView.top, 200, 15)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
     titleLable.centerY = 22;
    [whiteView addSubview:titleLable];
    
    return whiteView;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        TMGHelpTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"section0"];
        __weak typeof(self) weakSelf =  self;
        [cell.showButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelectedShowButton:sender withIndexPath:indexPath];
        }];
        TMGHelpModel * model = self.dataSocue[indexPath.section][indexPath.row];
        cell.model = model;
        return cell;
    }else{
        TMGMineTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"section1"];
        STTableEasyModel * model = self.dataSocue[indexPath.section][indexPath.row];
        cell.ghImageView.image = model.image;
        cell.lable.text = model.textString;
        return cell;
    }

}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    TMGHelpChildViewController * vc = [TMGHelpChildViewController new];
    if (indexPath.section == 1) {
        STTableEasyModel * model = self.dataSocue[indexPath.section][indexPath.row];
        if ([model.textString isEqualToString:@"买金和卖金"]) {
            vc.questionID = @"1";
        }
        if ([model.textString isEqualToString:@"存金和提金"]) {
            vc.questionID = @"2";
        }
        if ([model.textString isEqualToString:@"利息和收益"]) {
            vc.questionID = @"3";
        }
        if ([model.textString isEqualToString:@"支付和现金"]) {
            vc.questionID = @"4";
        }
        if ([model.textString isEqualToString:@"红包和优惠券"]) {
            vc.questionID = @"5";
        }
        if (vc && indexPath.section == 1) {
            [self.navigationController pushViewController:vc animated:YES];
        }
    }

    
}
#pragma mark --Private Method
- (void)cancelSelctedIndexPath{
    if (self.slectedindexPath) {
        TMGHelpModel * model = self.dataSocue[self.slectedindexPath.section ][self.slectedindexPath.row];
        model.isSelected = NO;
        [self.tableView reloadRowsAtIndexPaths:@[self.slectedindexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    STActionSheet * action = [[STActionSheet alloc] initWithTitle:nil buttonTitles:@[@"在线客服",@"拨打电话"] cancelTitle:@"取消" delegate:self];
    [action show];
}
- (void)onSelectedShowButton:(UIButton*)sender withIndexPath:(NSIndexPath*)indexPath{
    if (indexPath != self.slectedindexPath) {
        [self cancelSelctedIndexPath];
        self.slectedindexPath = indexPath;
        TMGHelpModel * model = self.dataSocue[indexPath.section ][indexPath.row];
        model.isSelected = ! model.isSelected ;
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }else{
        self.slectedindexPath = indexPath;
        TMGHelpModel * model = self.dataSocue[indexPath.section ][indexPath.row];
        model.isSelected = ! model.isSelected ;
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

}
#pragma mark --STActionSheetDelegate
- (void)actionSheet:(STActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
       [TMUtils gotoCustomServiceViewController];
    }
    if (buttonIndex == 1) {
        //拨打电话
        NSMutableString* str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",serviceCustomPhpne];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}
- (void)sendQuestionRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }

    [dic setObject:@"6" forKey:@"id"];

    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/usuallyQuestion"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"];
                                                 self.quesArray = [NSMutableArray new];
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGHelpModel * model = [TMGHelpModel mj_objectWithKeyValues:modelDic];
                                                     [self.quesArray addObject:model];
                                                 }
                                                 [self configDataSouce];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}

#pragma mark --Private Method
- (NSArray*)configTijinDataSouce{
    
    TMGHelpModel * model = [TMGHelpModel new];
    model.textString = @"提取黄金实物";
    model.detailString = @"1.选择黄金口袋产品和数量\n2.确定订单信息\n3.支付\n4.等待收货\n5.收到产品，提金完成";
    
    TMGHelpModel * model1 = [TMGHelpModel new];
    model1.textString = @"黄金存管业务详解？";
    model1.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    TMGHelpModel * model2 = [TMGHelpModel new];
    model2.textString = @"线下提取实物黄金业务详解";
    model2.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    TMGHelpModel * model3 = [TMGHelpModel new];
    model3.textString = @"线上提取实物黄金";
    model3.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    NSArray * sectionOne = @[model,model1,model2,model3];
    return @[sectionOne];
}
- (NSArray*)configBuyDataSouce{
    
    TMGHelpModel * model = [TMGHelpModel new];
    model.textString = @"提取黄金实物";
    model.detailString = @"1.选择黄金口袋产品和数量\n2.确定订单信息\n3.支付\n4.等待收货\n5.收到产品，提金完成";
    
    TMGHelpModel * model1 = [TMGHelpModel new];
    model1.textString = @"黄金存管业务详解？";
    model1.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    TMGHelpModel * model2 = [TMGHelpModel new];
    model2.textString = @"线下提取实物黄金业务详解";
    model2.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    TMGHelpModel * model3 = [TMGHelpModel new];
    model3.textString = @"线上提取实物黄金";
    model3.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    TMGHelpModel * model4 = [TMGHelpModel new];
    model4.textString = @"解套家中黄金存入黄金口袋生息";
    model4.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    NSArray * sectionOne = @[model,model1,model2,model3,model4];
    return @[sectionOne];
}
- (NSArray*)configShouyiDataSouce{
    
    TMGHelpModel * model = [TMGHelpModel new];
    model.textString = @"你在黄金口袋的收益";
    model.detailString = @"你在黄金口袋的收益的利息分为买金获利和利息获利两个部分\n买金获利指的是你在黄金口袋购买的黄金的盈利和亏损。因为黄金的价格是随市场行情波动";
    
    TMGHelpModel * model1 = [TMGHelpModel new];
    model1.textString = @"黄金利息是从哪里来";
    model1.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    TMGHelpModel * model2 = [TMGHelpModel new];
    model2.textString = @"黄金口袋的利息有多少";
    model2.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    TMGHelpModel * model3 = [TMGHelpModel new];
    model3.textString = @"线上黄金利息合适发放";
    model3.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    NSArray * sectionOne = @[model,model1,model2,model3];
    return @[sectionOne];
}
- (NSArray*)configPayDataSouce{
    
    TMGHelpModel * model = [TMGHelpModel new];
    model.textString = @"支付";
    model.detailString = @"你在进行余额账户充值或支付订单时候需要进行支付操作;\n在下单成功并跳转到支付页面后，您需要根据提示提供银行卡信息。预留手机号，以及身份证号码等信息进行付款，付款成功后订单生成并且保存在交易目录中";
    
    TMGHelpModel * model1 = [TMGHelpModel new];
    model1.textString = @"提现";
    model1.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    TMGHelpModel * model2 = [TMGHelpModel new];
    model2.textString = @"银行相关";
    model2.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";

    NSArray * sectionOne = @[model,model1,model2];
    return @[sectionOne];
}
- (NSArray*)configHongbaoDataSouce{
    
    TMGHelpModel * model = [TMGHelpModel new];
    model.textString = @"红包";
    model.detailString = @"买金券是黄金口袋发给永无的，在用户买金的时候，用于抵扣买金订单金额的电子券;\n在线上买金的时候，在订单页面选择可以使用的买金券，在线上线下门店购买实物黄金的时候";
    
    TMGHelpModel * model1 = [TMGHelpModel new];
    model1.textString = @"买金券使用说明";
    model1.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    TMGHelpModel * model2 = [TMGHelpModel new];
    model2.textString = @"加息券使用说明";
    model2.detailString = @"黄金口袋是行业临县的互联网黄金投资平台，致力于为用用提供超简单，高利益的黄金投资服务";
    
    NSArray * sectionOne = @[model,model1,model2];
    return @[sectionOne];
}
@end
